<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Performance Yes 🎉 | 人间熙攘，好久不见</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/favicon.ico">
    <script charset="utf-8" crossorigin="anonymous" src="/js/rainyday.js"></script>
    <meta name="description" content="人间熙攘，好久不见~ Magic 的博客">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <meta name="google-site-verification" content="cFm07VssWCh93-dPrep06FyYb3ST7MIfQnx9B6ZcYsQ">
    
    <link rel="preload" href="/assets/css/0.styles.fe1de732.css" as="style"><link rel="preload" href="/assets/js/app.fe0f04fe.js" as="script"><link rel="preload" href="/assets/js/3.8f0a3ee6.js" as="script"><link rel="preload" href="/assets/js/1.0d4da6ad.js" as="script"><link rel="preload" href="/assets/js/11.22795e1d.js" as="script"><link rel="prefetch" href="/assets/js/10.c5719ae1.js"><link rel="prefetch" href="/assets/js/12.35a1eee1.js"><link rel="prefetch" href="/assets/js/13.ef37c62d.js"><link rel="prefetch" href="/assets/js/14.f36a185b.js"><link rel="prefetch" href="/assets/js/15.06ade84c.js"><link rel="prefetch" href="/assets/js/16.3d06e6fb.js"><link rel="prefetch" href="/assets/js/17.7dd5742a.js"><link rel="prefetch" href="/assets/js/18.7bff1c11.js"><link rel="prefetch" href="/assets/js/19.7bb3239d.js"><link rel="prefetch" href="/assets/js/20.501cb431.js"><link rel="prefetch" href="/assets/js/21.8cfea759.js"><link rel="prefetch" href="/assets/js/22.ef6af803.js"><link rel="prefetch" href="/assets/js/23.8b263173.js"><link rel="prefetch" href="/assets/js/24.dd9f70b8.js"><link rel="prefetch" href="/assets/js/25.4ec78de4.js"><link rel="prefetch" href="/assets/js/26.8e7888cc.js"><link rel="prefetch" href="/assets/js/27.a78de168.js"><link rel="prefetch" href="/assets/js/28.9faf1a01.js"><link rel="prefetch" href="/assets/js/4.a1817f8d.js"><link rel="prefetch" href="/assets/js/5.779f467f.js"><link rel="prefetch" href="/assets/js/6.6a2cdf6b.js"><link rel="prefetch" href="/assets/js/7.ccf4b6c6.js"><link rel="prefetch" href="/assets/js/8.27e71aa7.js"><link rel="prefetch" href="/assets/js/9.6dcd52b0.js">
    <link rel="stylesheet" href="/assets/css/0.styles.fe1de732.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-2d5f533b><div data-v-2d5f533b><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-2d5f533b data-v-2d5f533b><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-2d5f533b data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>人间熙攘，好久不见</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Magic</span>
            
          <span data-v-64685f0e>2017 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-2d5f533b><header class="navbar" data-v-2d5f533b><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo/nav.png" alt="人间熙攘，好久不见" class="logo"> <span class="site-name">人间熙攘，好久不见</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/tools/" class="nav-link"><i class="iconfont undefined"></i>
  tools
</a></li><li class="dropdown-item"><!----> <a href="/categories/Zsh/" class="nav-link"><i class="iconfont undefined"></i>
  Zsh
</a></li><li class="dropdown-item"><!----> <a href="/categories/iTerm2/" class="nav-link"><i class="iconfont undefined"></i>
  iTerm2
</a></li><li class="dropdown-item"><!----> <a href="/categories/Front-end/" class="nav-link"><i class="iconfont undefined"></i>
  Front-end
</a></li><li class="dropdown-item"><!----> <a href="/categories/版本控制-Git/" class="nav-link"><i class="iconfont undefined"></i>
  版本控制-Git
</a></li><li class="dropdown-item"><!----> <a href="/categories/Rust/" class="nav-link"><i class="iconfont undefined"></i>
  Rust
</a></li><li class="dropdown-item"><!----> <a href="/categories/skia-safe/" class="nav-link"><i class="iconfont undefined"></i>
  skia-safe
</a></li><li class="dropdown-item"><!----> <a href="/categories/第三方对接/" class="nav-link"><i class="iconfont undefined"></i>
  第三方对接
</a></li><li class="dropdown-item"><!----> <a href="/categories/Powershell/" class="nav-link"><i class="iconfont undefined"></i>
  Powershell
</a></li><li class="dropdown-item"><!----> <a href="/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/categories/wsl2/" class="nav-link"><i class="iconfont undefined"></i>
  wsl2
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tags
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont undefined"></i>
      GitHub
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/magicLaLa/Magic.me" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/magicLaLa/blog" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Before
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-2d5f533b></div> <aside class="sidebar" data-v-2d5f533b><div class="personal-info-wrapper" data-v-ca798c94 data-v-2d5f533b><img src="/logo/avatar2.gif" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    Magic
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>18</h3> <h6 data-v-ca798c94>文章</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>11</h3> <h6 data-v-ca798c94>标签</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/tools/" class="nav-link"><i class="iconfont undefined"></i>
  tools
</a></li><li class="dropdown-item"><!----> <a href="/categories/Zsh/" class="nav-link"><i class="iconfont undefined"></i>
  Zsh
</a></li><li class="dropdown-item"><!----> <a href="/categories/iTerm2/" class="nav-link"><i class="iconfont undefined"></i>
  iTerm2
</a></li><li class="dropdown-item"><!----> <a href="/categories/Front-end/" class="nav-link"><i class="iconfont undefined"></i>
  Front-end
</a></li><li class="dropdown-item"><!----> <a href="/categories/版本控制-Git/" class="nav-link"><i class="iconfont undefined"></i>
  版本控制-Git
</a></li><li class="dropdown-item"><!----> <a href="/categories/Rust/" class="nav-link"><i class="iconfont undefined"></i>
  Rust
</a></li><li class="dropdown-item"><!----> <a href="/categories/skia-safe/" class="nav-link"><i class="iconfont undefined"></i>
  skia-safe
</a></li><li class="dropdown-item"><!----> <a href="/categories/第三方对接/" class="nav-link"><i class="iconfont undefined"></i>
  第三方对接
</a></li><li class="dropdown-item"><!----> <a href="/categories/Powershell/" class="nav-link"><i class="iconfont undefined"></i>
  Powershell
</a></li><li class="dropdown-item"><!----> <a href="/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/categories/wsl2/" class="nav-link"><i class="iconfont undefined"></i>
  wsl2
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tags
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont undefined"></i>
      GitHub
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/magicLaLa/Magic.me" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/magicLaLa/blog" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  Before
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>FrontEnd</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Contents/FrontEnd/ChromeExtensions.html" class="sidebar-link">使用 Vue + TypeScript 开发 Chrome Extensions 🐱‍👤</a></li><li><a href="/Contents/FrontEnd/DebounceAndthrottling.html" class="sidebar-link">Debounce Throttling ~</a></li><li><a href="/Contents/FrontEnd/Performance.html" aria-current="page" class="active sidebar-link">Performance Yes 🎉</a></li><li><a href="/Contents/FrontEnd/RollupAndTsToNpmPackage.html" class="sidebar-link">Rollup + Typescript =&gt; NPM Package</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>Performance Yes 🎉</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Magic</span>
            
          <span data-v-64685f0e>2017 - </span>
          2022
        </a></span></div></div> <div data-v-2d5f533b><main class="page"><div class="page-title" style="display:none;"><h1 class="title">Performance Yes 🎉</h1> <div data-v-3b7f5bdf><i class="iconfont reco-account" data-v-3b7f5bdf><span data-v-3b7f5bdf>Magic</span></i> <i class="iconfont reco-date" data-v-3b7f5bdf><span data-v-3b7f5bdf>2020-12-01</span></i> <i class="iconfont reco-eye" data-v-3b7f5bdf><span id="/Contents/FrontEnd/Performance.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-3b7f5bdf><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-3b7f5bdf><span class="tag-item" data-v-3b7f5bdf>Front-end</span><span class="tag-item" data-v-3b7f5bdf>Performance</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><div class="abstract-box"><p><img src="/Abstract/performance/one.png" alt="performance"></p></div> <h1 id="performance"><a href="#performance" class="header-anchor">#</a> Performance</h1> <h2 id="介绍"><a href="#介绍" class="header-anchor">#</a> 介绍</h2> <p>Performance 是一个做前端性能监控离不开的API，最好在页面完全加载完成之后再使用，因为很多值必须在页面完全加载之后才能得到。最简单的办法是在 window.onload 事件中读取各种数据。</p> <details class="custom-block details"><summary>可以先看一下个个环节的时间顺序：</summary> <p><img src="/assets/img/one.d9ced95d.png" alt="performance"></p></details> <h2 id="属性"><a href="#属性" class="header-anchor">#</a> 属性</h2> <h3 id="navigation"><a href="#navigation" class="header-anchor">#</a> navigation</h3> <p>告诉开发者当前页面是通过什么方式导航过来的，只有两个属性：<code>type</code>，<code>redirectCount</code>。<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceNavigation" target="_blank" rel="noopener noreferrer">具体看文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="memory"><a href="#memory" class="header-anchor">#</a> memory</h3> <p>描述内存多少，是在Chrome中添加的一个非标准属性。有三个属性：</p> <ol><li><code>usedJSHeapSize</code>： JS 对象（包括V8引擎内部对象）占用的内存，一定小于 totalJSHeapSize</li> <li><code>totalJSHeapSize</code>： 可使用的内存</li> <li><code>jsHeapSizeLimit</code>： 内存大小限制</li></ol> <h3 id="timing"><a href="#timing" class="header-anchor">#</a> timing</h3> <ol><li><code>navigationStart</code>: 在同一个浏览器上下文中，前一个网页（与当前页面不一定同域）unload 的时间戳，如果无前一个网页 unload ，则与 fetchStart 值相等;</li> <li><code>unloadEventStart</code>: 前一个网页（与当前页面同域）unload 的时间戳，如果无前一个网页 unload 或者前一个网页与当前页面不同域，则值为 0</li> <li><code>unloadEventEnd</code>:  和 unloadEventStart 相对应，返回前一个网页 unload 事件绑定的回调函数执行完毕的时间戳</li> <li><code>redirectStart</code>: 第一个 HTTP 重定向发生时的时间。有跳转且是同域名内的重定向才算，否则值为 0</li> <li><code>redirectEnd</code>: 最后一个 HTTP 重定向完成时的时间。有跳转且是同域名内的重定向才算，否则值为 0</li> <li><code>fetchStart</code>: 浏览器准备好使用 HTTP 请求抓取文档的时间，这发生在检查本地缓存之前</li> <li><code>domainLookupStart</code>: DNS 域名查询开始的时间，如果使用了本地缓存（即无 DNS 查询）或持久连接，则与 fetchStart 值相等</li> <li><code>domainLookupEnd</code>: DNS 域名查询完成的时间，如果使用了本地缓存（即无 DNS 查询）或持久连接，则与 fetchStart 值相等</li> <li><code>connectStart</code>: HTTP（TCP） 开始建立连接的时间，如果是持久连接，则与 fetchStart 值相等,如果在传输层发生了错误且重新建立连接，则这里显示的是新建立的连接开始的时间</li> <li><code>connectEnd</code>: HTTP（TCP） 完成建立连接的时间（完成握手），如果是持久连接，则与 fetchStart 值相等,如果在传输层发生了错误且重新建立连接，则这里显示的是新建立的连接完成的时间</li></ol> <div class="custom-block tip"><p class="custom-block-title">注意：</p> <p>这里握手结束，包括安全连接建立完成、SOCKS 授权通过</p></div> <ol><li><code>secureConnectionStart</code>: HTTPS 连接开始的时间，如果不是安全连接，则值为 0</li> <li><code>requestStart</code>: HTTP 请求读取真实文档开始的时间（完成建立连接），包括从本地读取缓存,连接错误重连时，这里显示的也是新建立连接的时间</li> <li><code>responseStart</code>: HTTP 开始接收响应的时间（获取到第一个字节），包括从本地读取缓存</li> <li><code>responseEnd</code>: HTTP 响应全部接收完成的时间（获取到最后一个字节），包括从本地读取缓存</li> <li><code>domLoading</code>: 开始解析渲染 DOM 树的时间，此时 Document.readyState 变为 loading，并将抛出 readystatechange 相关事件</li> <li><code>domInteractive</code>: 完成解析 DOM 树的时间，Document.readyState 变为 interactive，并将抛出 readystatechange 相关事件</li></ol> <div class="custom-block tip"><p class="custom-block-title">注意:</p> <p>这里只是 DOM 树解析完成，这时候并没有开始加载网页内的资源</p></div> <ol><li><code>domContentLoadedEventStart</code>: DOM 解析完成后，网页内资源加载开始的时间,文档发生 DOMContentLoaded事件的时间</li> <li><code>domContentLoadedEventEnd</code>: DOM 解析完成后，网页内资源加载完成的时间（如 JS 脚本加载执行完毕），文档的DOMContentLoaded 事件的结束时间</li> <li><code>domComplete</code>: DOM 树解析完成，且资源也准备就绪的时间，Document.readyState 变为 complete，并将抛出 readystatechange 相关事件</li> <li><code>loadEventStart</code>: load 事件发送给文档，也即 load 回调函数开始执行的时间,如果没有绑定 load 事件，值为 0</li> <li><code>loadEventEnd</code>: load 事件的回调函数执行完毕的时间,如果没有绑定 load 事件，值为 0</li></ol> <h2 id="方法"><a href="#方法" class="header-anchor">#</a> 方法</h2> <h3 id="getentries"><a href="#getentries" class="header-anchor">#</a> getEntries</h3> <p>获取所有资源请求的时间数据,这个函数返回一个按 <strong>startTime</strong> 排序的对象数组，数组成员除了会自动根据所请求资源的变化而改变以外，还可以用<code>mark()</code>、<code>measure()</code>方法自定义添加，该对象的属性中除了包含资源加载时间还有以下五个属性：</p> <ol><li><code>name</code>：资源名称，是资源的绝对路径或调用mark方法自定义的名称</li> <li><code>startTime</code>: 开始时间</li> <li><code>duration</code>：加载时间</li> <li><code>entryType</code>：资源类型，entryType类型不同数组中的对象结构也不同！</li> <li><code>initiatorType</code>：谁发起的请求</li></ol> <h4 id="entrytype"><a href="#entrytype" class="header-anchor">#</a> entryType</h4> <table><thead><tr><th>值</th> <th style="text-align:center;">该类型对象</th> <th style="text-align:center;">描述</th></tr></thead> <tbody><tr><td>mark</td> <td style="text-align:center;">PerformanceMark</td> <td style="text-align:center;">通过mark()方法添加到数组中的对象</td></tr> <tr><td>measure</td> <td style="text-align:center;">PerformanceMeasure</td> <td style="text-align:center;">通过measure()方法添加到数组中的对象</td></tr> <tr><td>paint</td> <td style="text-align:center;">PerformancePaintTiming</td> <td style="text-align:center;">值为<code>first-paint(首次绘制)</code>、<code>first-contentful-paint(首次内容绘制)</code>。</td></tr> <tr><td>resource</td> <td style="text-align:center;">PerformanceResourceTiming</td> <td style="text-align:center;">所有资源加载时间，用处最多</td></tr> <tr><td>navigation</td> <td style="text-align:center;">PerformanceNavigationTiming</td> <td style="text-align:center;">现除chrome和Opera外均不支持，导航相关信息</td></tr> <tr><td>frame</td> <td style="text-align:center;">PerformanceFrameTiming</td> <td style="text-align:center;">现浏览器均未支持</td></tr></tbody></table> <blockquote><p>可参照：<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceEntry/entryType" target="_blank" rel="noopener noreferrer">PerformanceEntry/entryType<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <h4 id="initiatortype"><a href="#initiatortype" class="header-anchor">#</a> initiatorType</h4> <table><thead><tr><th>发起对象</th> <th style="text-align:center;">值</th> <th style="text-align:center;">描述</th></tr></thead> <tbody><tr><td>a Element</td> <td style="text-align:center;"><code>link/script/img/iframe</code>等</td> <td style="text-align:center;">通过标签形式加载的资源，值是该节点名的小写形式</td></tr> <tr><td>a CSS resourc</td> <td style="text-align:center;"><code>css</code></td> <td style="text-align:center;">通过css样式加载的资源，比如<code>background: url</code>方式加载资源</td></tr> <tr><td>a XMLHttpRequest object</td> <td style="text-align:center;"><code>xmlhttprequest/fetch</code></td> <td style="text-align:center;">通过xhr加载的资源</td></tr> <tr><td>a PerformanceNavigationTiming object</td> <td style="text-align:center;"><code>navigation</code></td> <td style="text-align:center;">当对象是<code>PerformanceNavigationTiming</code>时返回</td></tr></tbody></table> <h3 id="getentriesbyname、getentriesbytype"><a href="#getentriesbyname、getentriesbytype" class="header-anchor">#</a> getEntriesByName、getEntriesByType</h3> <blockquote><p>getEntriesByName(name,type[optional])，getEntriesByType(type)</p></blockquote> <ul><li><code>name</code>: 想要筛选出的资源名</li> <li><code>type</code>: entryType的值中一个</li></ul> <p>返回值仍是一个数组，这个数组相当于<code>getEntries()</code>方法经过所填参数筛选后的一个子集</p> <h3 id="clearresourcetimings"><a href="#clearresourcetimings" class="header-anchor">#</a> clearResourceTimings</h3> <p>该方法无参数无返回值，可以清除目前所有<code>entryType</code>为 <code>resource</code> 的数据</p> <h3 id="mark、measure、clearmarks、clearmeasures"><a href="#mark、measure、clearmarks、clearmeasures" class="header-anchor">#</a> mark、measure、clearMarks、clearMeasures</h3> <blockquote><p>mark(name)、measure(name, startMark, endMark)、clearMarks()、clearMeasures()</p></blockquote> <p>用于做标记和清除标记，供用户自定义统计一些数据，比如某函数运行耗时等</p> <ol><li><code>name</code>: 自定义的名称，不要和<code>getEntries()</code>返回的数组中其他<code>name</code>重复</li> <li><code>startMark</code>: 作为开始时间的标记名称或<code>PerformanceTiming</code>的一个属性</li> <li><code>endMark</code>: 作为结束时间的标记名称或<code>PerformanceTiming</code>的一个属性</li></ol> <ul><li>创建标记：<code>mark(name)</code>；</li> <li>记录两个标记的时间间隔：<code>measure(name, startMark, endMark)</code>;</li> <li>清除指定标记：<code>window.performance.clearMarks(name)</code>;</li> <li>清除所有标记：<code>window.performance.clearMarks()</code>;</li> <li>清除指定记录间隔数据：<code>window.performance.clearMeasures(name)</code>;</li> <li>清除所有记录间隔数据：<code>window.performance.clearMeasures()</code>;</li></ul> <h3 id="now"><a href="#now" class="header-anchor">#</a> now</h3> <p><code>performance.now()</code>是当前时间与<code>performance.timing.navigationStart</code>的时间差，以 <strong>微秒（百万分之一秒）</strong> 为单位的时间，与 <code>Date.now()-performance.timing.navigationStart</code>的区别是 <strong>不受系统程序执行阻塞的影响</strong>，因此更加精准。</p> <h2 id="使用方式"><a href="#使用方式" class="header-anchor">#</a> 使用方式</h2> <h3 id="简单计算出网页性能数据"><a href="#简单计算出网页性能数据" class="header-anchor">#</a> 简单计算出网页性能数据</h3> <blockquote><p>单页应用：第一次进入页面时的数据或者是刷新当前页面的数据可以通过 onload 拿到</p></blockquote> <div class="language-js extra-class"><pre class="language-js"><code>window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token function">performance</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> performance <span class="token operator">=</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>performance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'浏览器不支持 performance 接口'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">const</span> timing <span class="token operator">=</span> performance<span class="token punctuation">.</span>timing<span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'统计模块性能时间：index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 写出具体模块名称</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'准备新页面时间耗时: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>fetchStart <span class="token operator">-</span> timing<span class="token punctuation">.</span>navigationStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Appcache 耗时: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>domainLookupStart <span class="token operator">-</span> timing<span class="token punctuation">.</span>fetchStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'重定向的时间: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>redirectEnd <span class="token operator">-</span> timing<span class="token punctuation">.</span>redirectStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'DNS 查询耗时: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>domainLookupEnd <span class="token operator">-</span> timing<span class="token punctuation">.</span>domainLookupStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'TCP连接耗时: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>connectEnd <span class="token operator">-</span> timing<span class="token punctuation">.</span>connectStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'request请求耗时: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>responseEnd <span class="token operator">-</span> timing<span class="token punctuation">.</span>requestStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'请求完毕至DOM加载: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>domInteractive <span class="token operator">-</span> timing<span class="token punctuation">.</span>responseEnd<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'解释dom树耗时: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>domInteractive <span class="token operator">-</span> timing<span class="token punctuation">.</span>domLoading<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'页面加载完成的时间: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>loadEventEnd <span class="token operator">-</span> timing<span class="token punctuation">.</span>navigationStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'白屏时间: '</span> <span class="token operator">+</span> <span class="token punctuation">(</span>timing<span class="token punctuation">.</span>domContentLoadedEventStart <span class="token operator">-</span> timing<span class="token punctuation">.</span>navigationStart<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'ms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-performance-mark-精确计算程序执行时间"><a href="#使用-performance-mark-精确计算程序执行时间" class="header-anchor">#</a> 使用 performance.mark 精确计算程序执行时间</h3> <p>我们可以使用 <code>performance.mark()</code>  标记各种时间戳，保存为各种测量值，便可以批量地分析这些数据了。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">randomFunc</span> <span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>n<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 生成一个随机数</span>
        n <span class="token operator">=</span> <span class="token operator">~</span><span class="token operator">~</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> nameStart <span class="token operator">=</span> <span class="token string">'markStart'</span> <span class="token operator">+</span> n<span class="token punctuation">;</span>
    <span class="token keyword">var</span> nameEnd   <span class="token operator">=</span> <span class="token string">'markEnd'</span> <span class="token operator">+</span> n<span class="token punctuation">;</span>
    <span class="token comment">// 函数执行前做个标记</span>
    window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">mark</span><span class="token punctuation">(</span>nameStart<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// do nothing</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 函数执行后再做个标记</span>
    window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">mark</span><span class="token punctuation">(</span>nameEnd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 然后测量这个两个标记间的时间距离，并保存起来</span>
    <span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string">'measureRandomFunc'</span> <span class="token operator">+</span> n<span class="token punctuation">;</span>
    window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">measure</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> nameStart<span class="token punctuation">,</span> nameEnd<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 执行三次看看</span>
<span class="token function">randomFunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">randomFunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 指定一个名字</span>
<span class="token function">randomFunc</span><span class="token punctuation">(</span><span class="token number">888</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 看下保存起来的标记 mark</span>
<span class="token keyword">var</span> marks <span class="token operator">=</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">getEntriesByType</span><span class="token punctuation">(</span><span class="token string">'mark'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>marks<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 看下保存起来的测量 measure</span>
<span class="token keyword">var</span> measure <span class="token operator">=</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">getEntriesByType</span><span class="token punctuation">(</span><span class="token string">'measure'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>measure<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 看下我们自定义的测量</span>
<span class="token keyword">var</span> entries <span class="token operator">=</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">getEntriesByName</span><span class="token punctuation">(</span><span class="token string">'measureRandomFunc888'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>entries<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 清除指定标记</span>
window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">clearMarks</span><span class="token punctuation">(</span><span class="token string">'markStart888'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 清除所有标记</span>
window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">clearMarks</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 清除指定测量</span>
window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">clearMeasures</span><span class="token punctuation">(</span><span class="token string">'measureRandomFunc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 清除所有测量</span>
window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">clearMeasures</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>可以把之前我们测量页面加载完成的时间 从 <code>timing.loadEventEnd-timing.navigationStart</code> 改为以下代码：</p> <div class="language-js extra-class"><pre class="language-js"><code>window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">measure</span><span class="token punctuation">(</span><span class="token string">'pageCompletion'</span><span class="token punctuation">,</span><span class="token string">'navigationStart'</span> <span class="token punctuation">,</span> <span class="token string">'loadEventEnd'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> pageCompletion <span class="token operator">=</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">getEntriesByName</span><span class="token punctuation">(</span><span class="token string">'pageCompletion'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>pageCompletion<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="分析每个请求具体的耗时情况"><a href="#分析每个请求具体的耗时情况" class="header-anchor">#</a> 分析每个请求具体的耗时情况</h3> <p>我们可以使用 <code>window.performance.getEntries</code> 来获取到单个静态资源从开始发出请求到获取响应之间各个阶段的<code>Timing</code></p> <blockquote><p>在非web页面本身的域名下，这些属性在默认都会返回 <strong>0</strong> 值: redirectStart、 redirectEnd、 domainLookupStart、domainLookupEnd、 connectStart、 connectEnd、secureConnectionStart、 requestStart、 responseStart...更多详情访问 <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Resource_Timing_API" target="_blank" rel="noopener noreferrer">https://developer.mozilla.org/zh-CN/docs/Web/API/Resource_Timing_API<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">PerformanceGetEntries</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span>  entryTimesList<span class="token operator">:</span> any<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">const</span> entryList <span class="token operator">=</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">getEntries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>entryList<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
  entryList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token operator">:</span> any<span class="token punctuation">,</span> index</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
     <span class="token keyword">const</span> templeObj<span class="token operator">:</span> any <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token comment">//  'navigation', 'script', 'css', 'fetch', 'xmlhttprequest', 'link', 'img'</span>
     <span class="token keyword">const</span> usefulType <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'xmlhttprequest'</span><span class="token punctuation">,</span> <span class="token string">'link'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
     <span class="token keyword">if</span> <span class="token punctuation">(</span>usefulType<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>initiatorType<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">//  console.log('item', item);</span>
      templeObj<span class="token punctuation">.</span>name <span class="token operator">=</span> item<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
      templeObj<span class="token punctuation">.</span>nextHopProtocol <span class="token operator">=</span> item<span class="token punctuation">.</span>nextHopProtocol<span class="token punctuation">;</span>
      <span class="token comment">// 解析dom树耗时</span>
      <span class="token comment">//  templeObj.domTime = item.domComplete - item.domInteractive;</span>
      <span class="token comment">// 白屏时间</span>
      <span class="token comment">// templeObj.baipingTime = item.domInteractive - item.fetchStart;</span>
      <span class="token comment">// dns查询耗时</span>
       templeObj<span class="token punctuation">.</span>dnsTime <span class="token operator">=</span> item<span class="token punctuation">.</span>domainLookupEnd <span class="token operator">-</span> item<span class="token punctuation">.</span>domainLookupStart<span class="token punctuation">;</span>
        <span class="token comment">// tcp链接耗时</span>
       templeObj<span class="token punctuation">.</span>tcpTime <span class="token operator">=</span> item<span class="token punctuation">.</span>connectEnd <span class="token operator">-</span> item<span class="token punctuation">.</span>connectStart<span class="token punctuation">;</span>
        <span class="token comment">// 请求时间</span>
       templeObj<span class="token punctuation">.</span>reqTime <span class="token operator">=</span> item<span class="token punctuation">.</span>responseEnd <span class="token operator">-</span> item<span class="token punctuation">.</span>responseStart<span class="token punctuation">;</span>
        <span class="token comment">// 重定向时间</span>
       templeObj<span class="token punctuation">.</span>redirectTime <span class="token operator">=</span> item<span class="token punctuation">.</span>redirectEnd <span class="token operator">-</span> item<span class="token punctuation">.</span>redirectStart<span class="token punctuation">;</span>
       entryTimesList<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>templeObj<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">table</span><span class="token punctuation">(</span>entryTimesList<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="扩展"><a href="#扩展" class="header-anchor">#</a> 扩展</h2> <h3 id="chrome下的-network之timing"><a href="#chrome下的-network之timing" class="header-anchor">#</a> Chrome下的 Network之Timing</h3> <p><img src="/assets/img/network-timing.67dccf73.png" alt="network-timing"></p> <p>以下是可能会在“timing”选项卡中看到的每个阶段的更多信息：</p> <ul><li><code>Queueing</code> 浏览器在以下情况下对请求进行排队:
<ul><li>有更高优先级的请求</li> <li>已为该来源打开了六个TCP连接，这是限制。仅适用于HTTP / 1.0和HTTP / 1.1。</li> <li>浏览器正在磁盘缓存中短暂分配空间</li></ul></li> <li><code>Stalled</code> 请求可能因为队列中描述的任何原因而停止</li> <li><code>DNS Lookup</code> 浏览器正在解析请求的IP地址</li> <li><code>Proxy negotiation</code> 浏览器正在与<a href="https://en.wikipedia.org/wiki/Proxy_server" target="_blank" rel="noopener noreferrer">代理服务器<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>协商请求</li> <li><code>Request sent</code>  正在发送请求</li> <li><code>ServiceWorker Preparation</code> 浏览器正在启动服务程序</li> <li><code>Request to ServiceWorker</code>  正在将请求发送给 service worker</li> <li><code>Waiting (TTFB)</code>  浏览器正在等待响应的第一个字节。TTFB代表到第一个字节的时间。此时间包括一次往返延迟和服务器准备响应所花费的时间</li> <li><code>Content Download</code>  浏览器正在接收响应</li> <li><code>Receiving Push</code> 浏览器正在通过HTTP / 2服务器推送接收此响应的数据</li> <li><code>Reading Push</code> 浏览器正在读取先前接收的本地数据</li></ul> <blockquote><p>更多相关信息可以到 <a href="https://developers.google.com/web/tools/chrome-devtools" target="_blank" rel="noopener noreferrer">chrome-devtools<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 查看</p></blockquote></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 人间熙攘，好久不见
    </div> <div class="operation" style="right:90px;bottom:40px;display:none;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><canvas id="vuepress-canvas-ribbon"></canvas></div></div>
    <script src="/assets/js/app.fe0f04fe.js" defer></script><script src="/assets/js/3.8f0a3ee6.js" defer></script><script src="/assets/js/1.0d4da6ad.js" defer></script><script src="/assets/js/11.22795e1d.js" defer></script>
  </body>
</html>
